from main.models import Medicine
from main import db
from sqlalchemy.exc import InvalidRequestError
from main.utils import JSONHelper
from datetime import datetime
from sqlalchemy import or_, and_
from flask import render_template

def insert(medicine):
    try:
        db.session.add(medicine)
        db.session.commit()
        return True
    except InvalidRequestError:
        db.session.rollback()
    except Exception as e:
        print(e)
        return False


# 传medicine查询warning
def query_warning(medicine):
    return db.session.query(Medicine).filter(Medicine.medicine_name == medicine).first()


def query_all():
    return db.session.query(Medicine).all()






if __name__ == '__main__':
    # file1 = open("/Users/jiahongzheng/PycharmProjects/healthy_me/main/药品名称.txt")
    # file2 = open("/Users/jiahongzheng/PycharmProjects/healthy_me/main/药品禁忌.txt")
    # n1 = file1.readlines()
    # n2 = file2.readlines()
    # for i in range(len(n1)):
    #     #print(n1[i])
    #     medicine  = Medicine(medicine_name=n1[i],warning=n2[i])
    #     insert(medicine)
    # #medicine = Medicine(medicine_name='test', warning='test')
    # #insert(medicine)
        print(query_warning("小儿氨酚烷胺颗粒").to_json())